]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
screenshot: save to an unique path when using a basename
authorCosimo Cecchi <cosimoc@gnome.org>
Wed, 2 Jan 2013 17:58:07 +0000 (18:58 +0100)
committerCosimo Cecchi <cosimoc@gnome.org>
Thu, 3 Jan 2013 11:56:45 +0000 (12:56 +0100)
When a basename is passed, avoid filename conflicts. The unique path is
returned by the API.

https://bugzilla.gnome.org/show_bug.cgi?id=688004

src/shell-screenshot.c

index b05550ecb08d9d96bdf91ca4c8580aaed7d7f745..4078ccd5612263c6080e1002a7091ff6ae67aee5 100644 (file)
@@ -77,13 +77,14 @@ on_screenshot_written (GObject *source,
 
 /* called in an I/O thread */
 static GOutputStream *
-get_stream_for_path (const gchar *path,
-                     const gchar *filename,
-                     gchar **filename_used)
+get_stream_for_unique_path (const gchar *path,
+                            const gchar *filename,
+                            gchar **filename_used)
 {
   GOutputStream *stream;
   GFile *file;
   gchar *real_path, *real_filename, *name, *ptr;
+  gint idx;
 
   ptr = g_strrstr (filename, ".png");
 
@@ -92,16 +93,33 @@ get_stream_for_path (const gchar *path,
   else
     real_filename = g_strdup (filename);
 
-  name = g_strdup_printf ("%s.png", real_filename);
-  real_path = g_build_filename (path, name, NULL);
-  g_free (name);
-  g_free (real_filename);
+  idx = 0;
+  real_path = NULL;
+
+  do
+    {
+      if (idx == 0)
+        name = g_strdup_printf ("%s.png", real_filename);
+      else
+        name = g_strdup_printf ("%s - %d.png", real_filename, idx);
+
+      real_path = g_build_filename (path, name, NULL);
+      g_free (name);
+
+      file = g_file_new_for_path (real_path);
+      stream = G_OUTPUT_STREAM (g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL));
+      g_object_unref (file);
 
-  file = g_file_new_for_path (real_path);
-  stream = G_OUTPUT_STREAM (g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL));
-  g_object_unref (file);
+      if (stream != NULL)
+        *filename_used = real_path;
+      else
+        g_free (real_path);
 
-  *filename_used = real_path;
+      idx++;
+    }
+  while (stream == NULL);
+
+  g_free (real_filename);
 
   return stream;
 }
@@ -121,7 +139,7 @@ get_stream_for_filename (const gchar *filename,
         return NULL;
     }
 
-  return get_stream_for_path (path, filename, filename_used);
+  return get_stream_for_unique_path (path, filename, filename_used);
 }
 
 static GOutputStream *